package com.molichuxing.gateway.api.services.impl;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.molichuxing.framework.dto.Paged;
import com.molichuxing.framework.exception.BizException;
import com.molichuxing.framework.result.ResponseResult;
import com.molichuxing.framework.utils.DateUtil;
import com.molichuxing.framework.utils.ObjectCastUtil;
import com.molichuxing.gateway.api.config.OptionalLogConfig;
import com.molichuxing.gateway.api.services.AdvertApiService;
import com.molichuxing.gateway.bff.advert.entity.request.create.AdvertCreateVo;
import com.molichuxing.gateway.bff.advert.entity.request.modify.AdvertModifyVo;
import com.molichuxing.gateway.bff.advert.entity.request.modify.AdvertStoreModifyVo;
import com.molichuxing.gateway.bff.advert.entity.response.AdvertDealerStoreVo;
import com.molichuxing.gateway.bff.advert.entity.response.AdvertVo;
import com.molichuxing.gateway.bff.advert.entity.response.BrandAndDealerVo;
import com.molichuxing.gateway.bff.advert.services.AdvertBffService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

@Api(tags = "广告相关接口")
@RestController
@RequestMapping(value = "/advert")
public class AdvertApiServiceImpl extends RootApiService implements AdvertApiService{

private static final Logger logger = LoggerFactory.getLogger(LogApiServiceImpl.class);
	
	@Resource
	private AdvertBffService advertBffService;
	
	/**
	 * 获取广告列表
	 * @param
	 * @return
	 */
	@Override
	@ApiOperation(value = "获取广告列表")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "name", value = "广告名称", required = false, dataType = "String"),
			@ApiImplicitParam(name = "position", value = "广告位置（1:终端屏保广告   2:APP开机广告）", required = false, dataType = "Integer"),
			@ApiImplicitParam(name = "startTime", value = "开始时间", required = false, dataType = "String"),
			@ApiImplicitParam(name = "endTime", value = "结束时间", required = false, dataType = "String"),
			@ApiImplicitParam(name = "pageNum", value = "页码", required = true, dataType = "Integer"),
			@ApiImplicitParam(name = "pageSize", value = "每页条数", required = true, dataType = "Integer")
	})
	@GetMapping(value = "/list")
	public ResponseResult<Paged<AdvertVo>> getAdvertList(@RequestParam Map<String, Object> map) {
		
		ResponseResult<Paged<AdvertVo>> result = new ResponseResult<>();
		try {
			Integer pageNum = ObjectCastUtil.castInteger(map.get("pageNum"));
			if(null==pageNum || pageNum < 1 ) {
				map.put("pageNum",1);
			}
			Integer pageSize = ObjectCastUtil.castInteger(map.get("pageSize"));
			if(null==pageSize || pageSize <= 0) {
				map.put("pageSize",10);
			}
			String startTime = ObjectCastUtil.castString(map.get("startTime"));
			if(StringUtils.isNoneBlank(startTime)) {
				map.put("startTime",DateUtil.parseDate(startTime).atStartOfDay());
			}
			String endTime = ObjectCastUtil.castString(map.get("endTime"));
			if(StringUtils.isNoneBlank(endTime)) {
				map.put("endTime", DateUtil.getDayEnd(DateUtil.parseDate(endTime).atStartOfDay()));
			}
			Paged<AdvertVo> advertList = advertBffService.getAdvertList(map);
			return result.success(advertList);
		}catch (BizException e) {
			result.error(e.getExceptionCode(),e.getMessage());
		}catch (IllegalArgumentException e) {
			result.error(400,e.getMessage());
		}catch (Exception e) {
			logger.error("[getAdvertList]获取广告列表异常", e);
			result.error("获取广告列表信息异常");
		}
		return result;
	}
	
	/**
	 * 获取广告详情
	 * @param
	 * @return
	 */
	@Override
	@ApiOperation(value = "获取广告详情")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "id", value = "广告id", required = true, dataType = "Integer")
	})
	@GetMapping(value = "/detail")
	public ResponseResult<AdvertVo> getAdvertById(@RequestParam Integer id) {
		
		ResponseResult<AdvertVo> result = new ResponseResult<>();
		try {
			AdvertVo advert = advertBffService.getAdvertById(id);
			return result.success(advert);
		}catch (BizException e) {
			result.error(e.getExceptionCode(),e.getMessage());
		}catch (IllegalArgumentException e) {
			result.error(400,e.getMessage());
		}catch (Exception e) {
			logger.error("[getAdvertById]获取广告详情异常", e);
			result.error("获取广告详情信息异常");
		}
		return result;
	}
	
	/**
	 * 获取品牌和经销商下拉列表
	 * @param
	 * @return
	 */
	@Override
	@ApiOperation(value = "获取品牌和经销商下拉列表")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "type", value = "1：品牌 2：经销商", required = true, dataType = "Integer")
	})
	@GetMapping(value = "/brand/list")
	public ResponseResult<List<BrandAndDealerVo>> getBrandAndDealerList(@RequestParam Integer type) {
		
		ResponseResult<List<BrandAndDealerVo>> result = new ResponseResult<>();
		try {
			List<BrandAndDealerVo> brandAndDealerList = advertBffService.getBrandAndDealerList(type);
			return result.success(brandAndDealerList);
		}catch (BizException e) {
			result.error(e.getExceptionCode(),e.getMessage());
		}catch (IllegalArgumentException e) {
			result.error(400,e.getMessage());
		}catch (Exception e) {
			logger.error("[getBrandAndDealerList]获取品牌和经销商下拉列表异常", e);
			result.error("获取品牌和经销商下拉列表异常");
		}
		return result;
	}
	
	/**
	 * 获取广告门店信息
	 * @param
	 * @return
	 */
	@Override
	@ApiOperation(value = "获取广告门店信息")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "type", value = "1：品牌 2：经销商", required = false, dataType = "Integer"),
			@ApiImplicitParam(name = "typeId", value = "品牌id或经销商id", required = false, dataType = "Integer"),
			@ApiImplicitParam(name = "advertId", value = "广告id", required = true, dataType = "Integer")
	})
	@GetMapping(value = "/store/list")
	public ResponseResult<AdvertDealerStoreVo> getDealerAndStoreList(@RequestParam(required=false) Integer type,@RequestParam(required=false) Integer typeId,@RequestParam Integer advertId) {
		
		ResponseResult<AdvertDealerStoreVo> result = new ResponseResult<>();
		try {
			AdvertDealerStoreVo vo = advertBffService.getDealerAndStoreList(type, typeId, advertId);
			return result.success(vo);
		}catch (BizException e) {
			result.error(e.getExceptionCode(),e.getMessage());
		}catch (IllegalArgumentException e) {
			result.error(400,e.getMessage());
		}catch (Exception e) {
			logger.error("[getDealerAndStoreList]获取广告门店信息异常", e);
			result.error("获取广告门店信息异常");
		}
		return result;
	}
	
	/**
	 * 添加广告
	 * @param createVo
	 * @return
	 */
	@Override
	@ApiOperation(value = "添加广告")
	@OptionalLogConfig(methods="添加广告")
	@PostMapping(value = "/create")
	public ResponseResult<Boolean> create(@RequestBody AdvertCreateVo createVo){
		ResponseResult<Boolean>  result = new ResponseResult<>();
		try {
			Boolean flag = advertBffService.create(createVo);
			if(!flag) {
				result.error("添加广告失败");
			}
			return result.success(flag);
		}catch (BizException e) {
			result.error(e.getExceptionCode(),e.getMessage());
		}catch (IllegalArgumentException e) {
			result.error(400,e.getMessage());
		}catch (Exception e) {
			logger.error("[create]添加广告信息异常", e);
			result.error("添加广告信息异常");
		}
		return result;
	}
	
	
	/**
	 * 编辑广告
	 * @param modifyVo
	 * @return
	 */
	@Override
	@ApiOperation(value = "编辑广告")
	@OptionalLogConfig(methods="编辑广告")
	@PostMapping(value = "/modify")
	public ResponseResult<Boolean> modify(@RequestBody AdvertModifyVo modifyVo){
		ResponseResult<Boolean>  result = new ResponseResult<>();
		try {
			Boolean flag = advertBffService.modify(modifyVo);
			if(!flag) {
				result.error("编辑广告失败");
			}
			return result.success(flag);
		}catch (BizException e) {
			result.error(e.getExceptionCode(),e.getMessage());
		}catch (IllegalArgumentException e) {
			result.error(400,e.getMessage());
		}catch (Exception e) {
			logger.error("[modify]编辑广告信息异常", e);
			result.error("编辑广告信息异常");
		}
		return result;
	}
	
	/**
	 * 保存广告和门店绑定关系接口
	 * @param storeIds
	 * @param advertId
	 * @return
	 */
	@Override
	@ApiOperation(value = "保存广告和门店绑定关系接口")
	@OptionalLogConfig(methods="投放门店")
	@PostMapping(value = "/store/create")
	public ResponseResult<Boolean> createAdvertStore(@RequestBody AdvertStoreModifyVo modifyVo){
		ResponseResult<Boolean>  result = new ResponseResult<>();
		try {
			Boolean flag = advertBffService.createAdvertStore(modifyVo.getStoreIds(), modifyVo.getAdvertId());
			if(!flag) {
				result.error("保存广告和门店绑定关系接口失败");
			}
			return result.success(flag);
		}catch (BizException e) {
			result.error(e.getExceptionCode(),e.getMessage());
		}catch (IllegalArgumentException e) {
			result.error(400,e.getMessage());
		}catch (Exception e) {
			logger.error("[createAdvertStore]保存广告和门店绑定关系接口信息异常", e);
			result.error("保存广告和门店绑定关系接口信息异常");
		}
		return result;
	}
}
